#include "pagespeedtest.h"
#include "ui_pagespeedtest.h"

PageSpeedTest::PageSpeedTest(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageSpeedTest)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_pKeyBoard = new SlimNumKeyBoard(this);
    m_pKeyBoard->move(220,550);
    m_pKeyBoard->setFocusPolicy(Qt::NoFocus);
    connect(m_pKeyBoard,SIGNAL(setNumber(int)),this,SLOT(input_slot(int)));
    connect(m_pKeyBoard,SIGNAL(backspace()),this,SLOT(backspace_slot()));
    connect(m_pKeyBoard,SIGNAL(cancel()),this,SLOT(cancel_slot()));
    m_pKeyBoard->hide();

    ui->lineEdit_TestSpeed->installEventFilter(this);
    setPageActive(false);

    m_lTestList.append(ui->ProInitSpeed);
    m_lTestList.append(ui->BrkInitSpeed);
    m_lTestList.append(ui->ProDistantce);
    m_lTestList.append(ui->BrkDistantce);
    m_lTestList.append(ui->ProDuration);
    m_lTestList.append(ui->BrkDuration);
    m_lTestList.append(ui->ProEndSpeed);
    m_lTestList.append(ui->BrkEndSpeed);
    m_lTestList.append(ui->Acceleration);
    m_lTestList.append(ui->Dcceleration);

    QRegExp rx1 = QRegExp("^([1-9]|[1-9][0-9]|100)$");
    QRegExpValidator *validator1 = new QRegExpValidator(rx1,this);
    ui->lineEdit_TestSpeed->setValidator(validator1);
}

PageSpeedTest::~PageSpeedTest()
{
    delete ui;
}

void PageSpeedTest::setPageActive(bool xActive)
{
    ui->Btn_Init->setEnabled(xActive);
    ui->Btn_Start->setEnabled(xActive);
}

void PageSpeedTest::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

bool PageSpeedTest::eventFilter(QObject *obj, QEvent *event)
{
    if(event->type() == QEvent::MouseButtonPress)
    {
        if(obj == ui->lineEdit_TestSpeed)
        {
            ui->lineEdit_TestSpeed->setStyleSheet("background-color: rgb(100,255,255); border: 1px solid white;font:20px 'Arial';");
            m_pKeyBoard->show();
        }
        else
            ui->lineEdit_TestSpeed->setStyleSheet("background-color: white; border: 1px solid white;font:20px 'Arial';");
    }
    return QWidget::eventFilter(obj, event);
}

void PageSpeedTest::input_slot(int number)
{
    QKeyEvent numKey(QEvent::KeyPress, (Qt::Key)(Qt::Key_0+number), Qt::NoModifier, QString::number(number));
    QApplication::sendEvent(ui->lineEdit_TestSpeed, &numKey);
}

void PageSpeedTest::backspace_slot()
{
    QKeyEvent backspaceKey(QEvent::KeyPress,Qt::Key_Backspace,Qt::NoModifier);
    QApplication::sendEvent(ui->lineEdit_TestSpeed,&backspaceKey);
}

void PageSpeedTest::cancel_slot()
{
    m_pKeyBoard->hide();
}

void PageSpeedTest::setTestStartSpeed(uint8_t index, float speed)
{
    m_lTestList[2*SpeedTest_InitSpeed+index]->setText(QString("%1").arg(speed/100));
}

void PageSpeedTest::setTestDistance(uint8_t index, float distance)
{
    m_lTestList[2*SpeedTest_Distance+index]->setText(QString("%1").arg(distance/10));
}

void PageSpeedTest::setTestEndSpeed(uint8_t index, float speed)
{
    m_lTestList[2*SpeedTest_EndSpeed+index]->setText(QString("%1").arg(speed/100));
}

void PageSpeedTest::setTestDuration(uint8_t index, float time)
{
    m_lTestList[2*SpeedTest_Duration+index]->setText(QString("%1").arg(time/100));
}

void PageSpeedTest::setAcceleration(uint8_t index, float speed)
{
    m_lTestList[2*SpeedTest_Acceleration+index]->setText(QString("%1").arg(speed/100));
}

void PageSpeedTest::on_Btn_Init_clicked()
{
    emit signal_speed_test_init(true);
    ui->Btn_Init->setEnabled(false);
    QTimer::singleShot(2000,this,[=](){
        emit signal_speed_test_init(false);
        ui->Btn_Init->setEnabled(true);
    });
}

void PageSpeedTest::on_Btn_Start_clicked(bool checked)
{
    emit signal_speed_test_start(checked);
}

uint8_t PageSpeedTest::getTestSpeed()
{
    if(ui->lineEdit_TestSpeed->text().isEmpty())
        return 0;
    return ui->lineEdit_TestSpeed->text().toUShort();
}
